#ifndef MESSAGE_FIFO_H
#define MESSAGE_FIFO_H

#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/time.h>
#include   <iostream>

#include "augmavconn/augmavconn.h"

#include "augmavconn/mTimer.h"

#define BUFSIZE     3//256
#define FIFOFULL    -1
#define FIFOEMPTY   -2
#define RWOK         0
#define OVERTOP     -3
#define Time_100    100//100ms
#define data_pack_num 10//FIFO中可容纳的数据包个数
#define FIFO_count 7  //FIFO的数目

class mTimer;
namespace augmavconn
{
    class AugMavConnInterface ;
};
typedef struct FifoType {//在本程序中FIFO为递减FIFO，出口在buffer[4],
    mavlink_message_t buffer[BUFSIZE]; /* Circular buffer *///直接改成一格存一个数据包
    unsigned int w_index;         /* Index of write *///当w_index=0时，FIFO满，当w_index=5时，FIFO为空

}*pFifoType;

typedef struct BufferType {
     mavlink_message_t buffer; /* Circular buffer */
    unsigned int length;       //记录buffer的长度
}*pBufferType;

typedef struct Mes_Time {
    uint8_t second;
    int msec;
}*pMes_Time;

/**********************基本的FIFO类***************************/
class Message_FIFO_WR  //:public QObject
{
    //Q_OBJECT
/*****************/
public:
    Message_FIFO_WR();//构造函数
    unsigned int get_index();//获得FIFO写指针
    bool Isempty();//判断FIFO是否为空
    bool Isfull();//判断FIFO是否为满
    void WriteFIFO( mavlink_message_t data);//写入一个mavlink_message_t数据
    void ReadFIFO( mavlink_message_t* dataout);//读出一个mavlink_message_t数据
    uint8_t data_count;                //FIFO发送数据包的次数

protected:
    FifoType fifotype;
    pFifoType priv;
    void move();//移动FIFO内部数据
private:
    bool empty;//FIFO空标志
    bool full;//FIFO满标志
};

/**********************可定时发送的FIFO类***************************/
class Message_FIFO_WR_T :public Message_FIFO_WR
{
public:
    Message_FIFO_WR_T();//构造函数
    void Initial(int time,int id);//初始化写入定时时间和ID号
    void set();//定时器信号与槽函数连接

    void connect();

    pBufferType pBuffer;                 //用来加载统一输出的Buffer
    mTimer * Timer;                      //用来加载统一的时钟
    Mes_Time message_time[BUFSIZE];
    uint8_t mes_tcount;
    uint8_t message_len;
    void send_data();                   //定时器的槽函数
    int getInterval();
    int getID();
    void cls_Tcount();
    uint8_t gettime_num();
    uint8_t getTimer_count();
    void Timer_count_add();

    void get_outmessage(mavlink_message_t* pmavlink_message);
    void set_outmessage(uint8_t flag,uint8_t dex);

protected:
    uint8_t Timer_count;               //定时器响应的次数
    mavlink_message_t outmessage;

private:
    uint8_t time_num;                  //定时的时间=time_num*100ms
    int ID;                        //FIFO的ID号
    int Interval;
    bool read_flag;

};

/**********************非周期的FIFO类***************************/
class Message_FIFO_WR_T_Aperiodic :public  Message_FIFO_WR_T//非周期FIFO
{
public:
    Message_FIFO_WR_T_Aperiodic();
    void WriteFIFO(mavlink_message_t data);//重构写函数，可以发送信号
    void ApFIFO_Isempty();

};


void copy_mavmessage(mavlink_message_t datain,mavlink_message_t *dataout);

using namespace augmavconn;
extern augmavconn::AugMavConnInterface* augptr;
extern void Write_Buffer(uint8_t ID,mavlink_message_t buffer);
void   SetDataHandler( augmavconn::AugMavConnInterface* ptr);
#endif // MESSAGE_FIFO_H
